<?php
/**
 * Created by PhpStorm.
 * User: CS
 * Date: 2018/11/28
 * Time: 18:28
 */

namespace app\common;


use think\Db;
use think\model\concern\SoftDelete;

trait Base
{
    //软删除
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    //定义查询一条数据方法
    public function getOne($id)
    {
        return self::findOrFail($id);
    }

    //定义查询多条数据方法
    public function getMore($arr)
    {
        $offset = $arr['offset'] ?? 0;
        $limit = $arr['limit'] ?? config('api.limit');
        $wd = $arr['wd'] ?? '';
        $where = [];
        if(!empty($wd)){
            $where[] = ['title','like',"%$wd%"];
        }
        $limit = min(config('api.maxlist'),$limit);
        return strdecode(self::where($where)->limit($offset,$limit)->selectOrFail());
    }

    //定义新增一条数据方法
    public function saveOne($arr)
    {
        return self::create(strcode($arr));
    }

    //定义修改一条数据方法
    public function editOne($arr,$id)
    {
        $res = self::find($id);
        return $res ? $res->save(strcode($arr)) : [];

    }

    //定义删除一条数据方法
    public function delOne($id)
    {
        return self::destroy($id);
    }

    //点赞方法,cl字段名
    public function dianzan($id,$cl)
    {
        $str = $cl . '+1';
        return $this->where('id',$id)->update([$cl=>Db::raw($str)]);
    }
}